﻿/*
对话框
基于 bootstrap-modal.js
author:zengyy 
date:2003-5-19
*/
(function ($) {
    $.dialog = function (opts) {
        return top.__openDlg(opts);
    };
    $.dialog.close = function (returnValue) {
        top.__closeDlg(window, returnValue);
    }
    $.alert = function (content, title, onOK) {
        return top.__openDlg({
            title: title,
            content: content,
            callback: onOK,
            width: 350,
            keyboard: true,
            buttons: {
                "确定": {
                    "class": "btn btn-primary",
                    "data-dismiss": 'modal',
                    "value": "ok"
                }
            }
        });
    };
    $.confirm = function (content, title, onOK) {
        return top.__openDlg({
            title: title,
            content: content,
            callback: onOK,
            width: 350,
            keyboard: true,
            buttons: {
                "取消": {
                    "class": "btn",
                    "data-dismiss": 'modal'
                },
                "确定": {
                    "class": "btn btn-primary",
                    "value": "ok"
                }
            }
        });
    };
    //在顶层窗口增加__openDlg函数 
    if (!$.isFunction(window.top.__openDlg)) {
        window.top.__openDlg = function (opts) {
            opts = $.extend({ 
                buttons: {
                    "取消": {
                        "class": "btn",
                        "data-dismiss": 'modal'
                    },
                    "保存": {
                        "class": "btn btn-primary",
                        "value": "ok"
                    }
                },
                content: "",
                onDialogClose: "onDialogClose"
            }, opts || {});

            //生成对话框
            var dlgid = ("dialog_" + Math.random()).replace(".", "");
            var dlghtml = "<div id='" + dlgid + "' class='modal hide fade' data-backdrop='static'>";
            dlghtml += "<div class='modal-header'>";
            dlghtml += "<button type='button' class='close' data-dismiss='modal' aria-hidden='true' title='关闭'>&times;</button><h3>" + (opts.title||"") + "</h3>";
            dlghtml += "</div>";
            if (opts.url)
                dlghtml += "<iframe src='" + opts.url + "' class='modal-body' frameborder='0'></iframe>";
            else
                dlghtml += "<div class='modal-body' style='padding:10px auto!important'>" + opts.content + "</div>";
            dlghtml += "<div class='modal-footer'><div class='modal-toolbar' style='float:left'></div></div>";
            dlghtml += "</div>";
            var $dlg = $(dlghtml);
            //添加按钮
            for (var btn in opts.buttons) {
                $btn = $("<a>" + btn + "</a>").attr(opts.buttons[btn]);
                if ($btn.val()) {
                    $btn.click(function () {
                        var result = null;
                        if (opts.url) {
                            var onDialogClose = $(this).parent().prev("iframe")[0].contentWindow[opts.onDialogClose];
                            if ($.isFunction(onDialogClose)) {
                                result = onDialogClose.call($dlg[0], $(this).val());
                            }
                        } else {
                            result = $(this).val();
                        }

                        if (result !== undefined) {
                            $dlg.data("returnValue", result).modal("hide");
                        }
                    });
                }
                $btn.appendTo($(".modal-footer", $dlg));
            }
            //扩展控件
            if (opts.toolbar) {
                var $toolbar = typeof (opts.toolbar) == "string" ? $("#" + opts.toolbar) : opts.toolbar;
                $toolbar.clone(true).appendTo($(".modal-footer .modal-toolbar", $dlg)).show();
            }
            //窗口拖动
            if ($dlg.draggable) {
                $dlg.draggable({ handle: ".modal-header,.modal-footer", cursor: "move" })
                .children(".modal-header").css("cursor", "move"); //窗口拖动(需要包含jquery-ui的拖动功能)
            }
            //定位  
            $dlg.height(opts.height).width(opts.width);
            if (opts.url) {  //设置iframe 高度
                var height = $dlg.height() - $dlg.children(".modal-header").height() - $dlg.children(".modal-footer").height() - 42;
                var $iframe = $dlg.children(".modal-body");
                $iframe.css({ "height": height, "width": "99%" });
                if (opts.loading) {  //调用加载等待函数
                    opts.loading.call($iframe[0]);
                }
                if (!opts.title) { // 如果不指定标题，则采用对话框页面document的title
                    $iframe.on("load", function () { 
                        $(".modal-header>h3", $dlg).text($iframe[0].contentWindow.document.title);
                    })
                }
            }
            var top = ($(window).height() - $dlg.height()) / 2;
            top = top > 100 ? 100 : top;
            var left = ($(window).width() - $dlg.width()) / 2;
            $dlg.css({ "top": top, "left": left, "margin": 0, "position": "absolute" });
            //弹出对话框 
            $dlg.data("callback", opts.callback).modal({ show: true, keyboard: true })
			.on("hidden", function () {
			    if ($.isFunction($(this).data("callback")) && $(this).data("returnValue") !== undefined) {
			        $(this).data("callback").call(this, $(this).data("returnValue"));
			    }
			    $(this).remove();
			});

            return $dlg;
        };

        // 关闭窗口
        window.top.__closeDlg = function (win, returnValue) {
            $("iframe", top.document).filter(function () {
                return this.contentWindow == win;
            }).parent().data("returnValue", returnValue).modal("hide");
        }
    }
})(jQuery)